4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
18 using Microsoft
.JScript
.Vsa
;
20 using System
.Reflection
;
21 using System
.Reflection
.Emit
;
23 public sealed class Package
: AST
{
25 private ASTList classList
;
26 private PackageScope scope
;
28 internal Package(String name
, AST id
, ASTList classList
, Context context
)
31 this.classList
= classList
;
32 this.scope
= (PackageScope
)Globals
.ScopeStack
.Peek();
33 this.scope
.owner
= this;
34 this.Engine
.AddPackage(this.scope
);
35 Lookup simpleId
= id
as Lookup
;
37 simpleId
.EvaluateAsWrappedNamespace(true);
39 Member qualifiedId
= id
as Member
;
40 if (qualifiedId
!= null)
41 qualifiedId
.EvaluateAsWrappedNamespace(true);
45 internal override Object
Evaluate(){
46 Globals
.ScopeStack
.Push(this.scope
);
48 for (int i
= 0, n
= this.classList
.count
; i
< n
; i
++)
49 this.classList
[i
].Evaluate();
50 return new Completion();
52 Globals
.ScopeStack
.Pop();
56 public static void JScriptPackage(String rootName
, VsaEngine engine
){
57 GlobalScope scope
= ((IActivationObject
)engine
.ScriptObjectStackTop()).GetGlobalScope();
58 FieldInfo field
= scope
.GetLocalField(rootName
);
60 field
= scope
.AddNewField(rootName
, Namespace
.GetNamespace(rootName
, engine
), FieldAttributes
.Public
|FieldAttributes
.Literal
);
63 internal void MergeWith(Package p
){
64 for (int i
= 0, n
= p
.classList
.count
; i
< n
; i
++)
65 this.classList
.Append(p
.classList
[i
]);
66 this.scope
.MergeWith(p
.scope
);
69 internal override AST
PartiallyEvaluate(){
70 this.scope
.AddOwnName();
71 Globals
.ScopeStack
.Push(this.scope
);
73 for (int i
= 0, n
= this.classList
.count
; i
< n
; i
++)
74 this.classList
[i
].PartiallyEvaluate();
77 Globals
.ScopeStack
.Pop();
81 internal override void TranslateToIL(ILGenerator il
, Type rtype
){
82 Globals
.ScopeStack
.Push(this.scope
);
83 for (int i
= 0, n
= this.classList
.count
; i
< n
; i
++)
84 this.classList
[i
].TranslateToIL(il
, Typeob
.Void
);
85 Globals
.ScopeStack
.Pop();
88 internal override void TranslateToILInitializer(ILGenerator il
){
89 String root
= this.name
;
90 int j
= root
.IndexOf('.');
92 root
= root
.Substring(0, j
);
93 il
.Emit(OpCodes
.Ldstr
, root
);
94 this.EmitILToLoadEngine(il
);
95 il
.Emit(OpCodes
.Call
, CompilerGlobals
.jScriptPackageMethod
);
96 Globals
.ScopeStack
.Push(this.scope
);
97 for (int i
= 0, n
= this.classList
.count
; i
< n
; i
++)
98 this.classList
[i
].TranslateToILInitializer(il
);
99 Globals
.ScopeStack
.Pop();
102 internal override Context
GetFirstExecutableContext(){